<?xml version="1.0" ?>

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
						xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd"
                  version="1.0">

   <xsl:output method="text"/>

	<xsl:include href="../../common.xsl" />
	<xsl:include href="../../convert.xsl" />

   <xsl:template match="lzc:project"><xsl:apply-templates select="lzc:tables/lzc:table[ ( @compiling ) ]" /></xsl:template>

   <xsl:template match="lzc:table[ @compiling ]">
			<xsl:variable name="v_ObjectName">
				<xsl:value-of select="@name" />
			</xsl:variable>
			<xsl:variable name="v_ObjectLogicalName">
				<xsl:value-of select="@logical-name" />
			</xsl:variable>
         <xsl:variable name="v_ClassName">
            <xsl:value-of select="@name" />
			 <xsl:text>Base</xsl:text>
         </xsl:variable>
         <xsl:variable name="v_collections" select="lzc:collections/lzc:collection" />
         <xsl:variable name="v_NamespacePrefix">
				<xsl:if test="/lzc:project/@namespaceprefix">
					<xsl:value-of select="/lzc:project/@namespaceprefix" />
					<xsl:text>:</xsl:text>
				</xsl:if>
         </xsl:variable>
         <xsl:variable name="v_RuntimeNamespace">
				<xsl:if test="ancestor::lzc:project/@defaultnamespace">
					<xsl:value-of select="ancestor::lzc:project/@defaultnamespace"/>
				</xsl:if>
         </xsl:variable>
         <xsl:variable name="v_BusinessConceptNamespace">
				<xsl:if test="/lzc:project/@businessconcept">
					<xsl:text>.</xsl:text>
					<xsl:value-of select="/lzc:project/@businessconcept" />
				</xsl:if>
         </xsl:variable>#/usr/bin/python
#
# Copyright 2012 Google Inc.  All Rights Reserved.
"""A set of data access classes for the <xsl:value-of select="$v_ClassName" /> entity."""


__author__ = 'joemu@google.com (Joe Allan Muharsky)'

import cgi
import data_access_base

from google3.storage.speckle.python.api import rdbms_sql as rdbms


class <xsl:value-of select="$v_ClassName" />(data_access_base.DataAccessBase):
  """<xsl:value-of select="lzc:description" />"""

  def __init__(self, connection=None):
    super(<xsl:value-of select="$v_ClassName" />, self).__init__(connection=connection)
<xsl:apply-templates select="lzc:methods/lzc:method" />

</xsl:template>

   <xsl:template match="lzc:method">
      <xsl:variable name="args" select="lzc:method-params/lzc:method-param[@direction='in']" />
      <xsl:variable name="returns" select="lzc:method-params/lzc:method-param[@direction='out' and @name='id']" />
      <xsl:variable name="results" select="lzc:method-results//lzc:method-result" />
  def <xsl:value-of select="@object-name" />
      <xsl:call-template name="createParams">
         <xsl:with-param name="fields" select="$args" />
      </xsl:call-template><xsl:text>:
    """</xsl:text><xsl:value-of select="lzc:description" />
<xsl:if test="$args or $returns">
<xsl:call-template name="documentFields">
  <xsl:with-param name="title">Args</xsl:with-param>
  <xsl:with-param name="fields" select="$args" />
</xsl:call-template>
<xsl:call-template name="documentFields">
  <xsl:with-param name="title">Returns</xsl:with-param>
  <xsl:with-param name="fields" select="$returns" />
</xsl:call-template>
    """
</xsl:if>
<xsl:if test="not($args or $returns)">"""
</xsl:if>
    sql = 'CALL <xsl:value-of select="@db-name" />
    <xsl:text>(</xsl:text>
    <xsl:for-each select="$args">
      <xsl:if test="position() &gt; 1">, </xsl:if>
      <xsl:choose>
        <xsl:when test="@type = 'Varchar'">
          <xsl:text>&quot;%(</xsl:text>
          <xsl:value-of select="@dbname" />
          <xsl:text>)s&quot;</xsl:text>
        </xsl:when>
        <xsl:when test="@type = 'Bit'">
          <xsl:text>%(</xsl:text>
          <xsl:value-of select="@dbname" />
          <xsl:text>)d</xsl:text>
        </xsl:when>
        <xsl:when test="@type = 'Int'">
          <xsl:text>%(</xsl:text>
          <xsl:value-of select="@dbname" />
          <xsl:text>)d</xsl:text>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
    <xsl:text>)'
    execsql = sql % {</xsl:text>
    <xsl:for-each select="$args">
      <xsl:if test="position() &gt; 1">, </xsl:if>
      <xsl:text>'</xsl:text>
      <xsl:value-of select="@dbname" />
      <xsl:text>': </xsl:text>
      <xsl:value-of select="@dbname" />
    </xsl:for-each>
    <xsl:text>}

    self.cursor.execute(execsql)
</xsl:text>

<xsl:if test="$returns">
    result = self.cursor.fetchone()
<xsl:for-each select="$returns"><xsl:text>
    </xsl:text><xsl:value-of select="@dbname" /> = result[<xsl:value-of select="position() - 1" />]
</xsl:for-each>
    return <xsl:for-each select="$returns">
    <xsl:if test="position &gt; 1">, </xsl:if>
    <xsl:value-of select="@dbname" />
</xsl:for-each>
</xsl:if>
<xsl:if test="$results and @methodtype = 'List'">
    results = []

    for row in self.cursor.fetchall():
      result_row = {}
<xsl:for-each select="$results"><xsl:text>
      result_row['</xsl:text><xsl:value-of select="@dbname" />'] = row[<xsl:value-of select="position() - 1" />]</xsl:for-each>

      results.append(result_row)

    return results
</xsl:if>
<xsl:if test="$results and @methodtype = 'Get'">
    result = {}

    row = self.cursor.fetchone()
<xsl:for-each select="$results"><xsl:text>
    result['</xsl:text><xsl:value-of select="@dbname" />'] = row[<xsl:value-of select="position() - 1" />]</xsl:for-each>

    return result
</xsl:if>
<xsl:text>
</xsl:text>
   </xsl:template>

   <xsl:template name="createParams">
      <xsl:param name="fields" />
      <xsl:text>(self</xsl:text>
      <xsl:for-each select="$fields">
        <xsl:text>, </xsl:text>
        <xsl:value-of select="@dbname" />
        </xsl:for-each>
      <xsl:text>)</xsl:text>
   </xsl:template>

   <xsl:template name="documentFields">
      <xsl:param name="title" />
      <xsl:param name="fields" />
      <xsl:if test="count($fields) &gt; 0"><xsl:text>

      </xsl:text>
        <xsl:value-of select="$title" /><xsl:text>:</xsl:text>
      </xsl:if>
      <xsl:for-each select="$fields">
        <xsl:text>
        </xsl:text>
        <xsl:value-of select="@dbname" />
        <xsl:text>: </xsl:text>
        <xsl:value-of select="lzc:description" />
      </xsl:for-each>
      <xsl:if test="count($fields) &gt; 0">
      </xsl:if>
   </xsl:template>

</xsl:stylesheet>